package com.simpletaskapp.persistence;
 
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.simpletaskapp.R;
 
public class TaskSQLiteHelper extends SQLiteOpenHelper {
 
	private static final String DATABASE_NAME = "Tasks.db";
	
	private static final int DATABASE_VERSION = 5;
	
	public static final String TASK_TABLE = "TASK";
	
	private Context context;
	
	private static final String DATABASE_CREATE = "CREATE TABLE " + TASK_TABLE + "(" 
    		+ TaskColumns.ID_COLUMN.getName() + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
    		+ TaskColumns.DESCRIPTION_COLUMN.getName() + "  TEXT  NOT NULL, " 
    		+ TaskColumns.CREATION_DATE_COLUMN.getName() + " TEXT NOT NULL, " 
			+ TaskColumns.DONE_COLUMN.getName() + " INTEGER);";
    
	public TaskSQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        String creationDate = dateFormat.format(new Date());
        
        String initialImportMessage = context.getString(R.string.sample);
        
        String initialImport = "INSERT INTO " + TASK_TABLE + " (" 
        		+ TaskColumns.DESCRIPTION_COLUMN.getName() + ","
        		+ TaskColumns.CREATION_DATE_COLUMN.getName() + ","
        		+ TaskColumns.DONE_COLUMN.getName() + ") VALUES ('" 
        		+ initialImportMessage + "','"
        		+ creationDate + "',"
        		+ "1);";
        
        db.execSQL(initialImport);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
        db.execSQL("DROP TABLE IF EXISTS " + TASK_TABLE);
        onCreate(db);
    }
}